<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>银行流水导入配置</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 20px;
            background-color: #f5f5f5;
        }
        .container {
            max-width: 1200px;
            margin: 0 auto;
            background-color: white;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        h1 {
            color: #333;
            text-align: center;
        }
        .form-group {
            margin-bottom: 15px;
        }
        label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
        }
        input, select {
            width: 100%;
            padding: 8px;
            border: 1px solid #ddd;
            border-radius: 4px;
            box-sizing: border-box;
        }
        button {
            background-color: #007bff;
            color: white;
            padding: 8px 16px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 14px;
            margin-right: 10px;
        }
        button:hover {
            background-color: #0056b3;
        }
        .mapping-table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 20px;
        }
        .mapping-table th, .mapping-table td {
            border: 1px solid #ddd;
            padding: 12px;
            text-align: left;
        }
        .mapping-table th {
            background-color: #f8f9fa;
            font-weight: bold;
        }
        .mapping-table tr:nth-child(even) {
            background-color: #f8f9fa;
        }
        .mapping-table tr:hover {
            background-color: #e9ecef;
        }
        .action-buttons {
            margin: 20px 0;
            text-align: center;
        }
        .preview-section {
            margin-top: 30px;
        }
        .preview-table {
            width: 100%;
            border-collapse: collapse;
        }
        .preview-table th, .preview-table td {
            border: 1px solid #ddd;
            padding: 8px;
            text-align: left;
        }
        .preview-table th {
            background-color: #f8f9fa;
        }
        .field-type-info {
            font-size: 12px;
            color: #666;
            margin-top: 5px;
        }
        .uploaded-file-info {
            color: #28a745;
            font-weight: bold;
            margin: 10px 0;
        }
        .system-field-select option[value=""] {
            color: #999;
        }
        .header-row-index {
            color: #007bff;
            font-weight: bold;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>银行流水导入配置</h1>
        
        <form id="configForm">
            <div class="form-group">
                <label for="configName">配置名称:</label>
                <input type="text" id="configName" name="configName" required>
            </div>
            
            <div class="form-group">
                <label for="bankType">银行类型:</label>
                <select id="bankType" name="bankType" required>
                    <option value="">请选择银行类型</option>
                    <option value="BOC">中国银行</option>
                    <option value="CCB">建设银行</option>
                    <option value="ABC">农业银行</option>
                    <option value="ICBC">工商银行</option>
                    <option value="WECHAT">微信</option>
                    <option value="ALIPAY">支付宝</option>
                    <option value="WFBC">潍坊银行</option>
                    <option value="SCRCU">农村信用社</option>
                    <option value="PSBC">邮政银行</option>
                </select>
            </div>
            
            <div class="form-group">
                <label for="fileType">文件类型:</label>
                <select id="fileType" name="fileType" required>
                    <option value="Excel">Excel</option>
                    <option value="CSV">CSV</option>
                </select>
            </div>
            
            <div class="form-group">
                <label for="remark">备注:</label>
                <input type="text" id="remark" name="remark">
            </div>
            
            <button type="submit">保存配置</button>
        </form>
        
        <div class="preview-section">
            <h2>字段映射配置</h2>
            <p>请上传一个银行流水文件以预览字段，并配置字段映射关系：</p>
            
            <div class="form-group">
                <label for="bankFile">选择银行流水文件:</label>
                <input type="file" id="bankFile" name="bankFile" accept=".xlsx,.xls">
            </div>
            
            <div id="uploadedFileInfo" class="uploaded-file-info" style="display: none;">
                已上传文件: <span id="fileName"></span>
            </div>
            
            <div id="headerRowIndexInfo" class="header-row-index" style="display: none;">
                表头行索引: <span id="headerRowIndex"></span>
            </div>
            
            <div class="field-type-info">
                <p><strong>字段类型说明：</strong></p>
                <ul>
                    <li><strong>普通字段</strong>：常规字段映射</li>
                    <li><strong>收入金额字段</strong>：表示收入金额的字段（情况一）</li>
                    <li><strong>支出金额字段</strong>：表示支出金额的字段（情况一）</li>
                    <li><strong>类型标识字段</strong>：表示交易类型的字段，如"借/贷"、"来账/往账"（情况二、三）</li>
                    <li><strong>付款人信息字段</strong>：表示付款人相关信息的字段（交易类型为收入时使用）</li>
                    <li><strong>收款人信息字段</strong>：表示收款人相关信息的字段（交易类型为支出时使用）</li>
                </ul>
            </div>
            
            <table class="mapping-table" id="mappingTable">
                <thead>
                    <tr>
                        <th>文件列索引</th>
                        <th>文件列名称</th>
                        <th>映射到系统字段</th>
                        <th>字段类型</th>
                    </tr>
                </thead>
                <tbody>
                    <!-- 动态生成映射行 -->
                </tbody>
            </table>
            
            <div class="action-buttons">
                <button id="saveMappingBtn">保存字段映射</button>
            </div>
        </div>
        
        <div class="preview-section">
            <h2>导入预览</h2>
            <table class="preview-table" id="previewTable">
                <thead>
                    <tr>
                        <th>交易时间</th>
                        <th>交易金额</th>
                        <th>账户余额</th>
                        <th>交易类型</th>
                        <th>对方银行</th>
                        <th>对方卡号</th>
                        <th>交易对手</th>
                        <th>交易描述</th>
                    </tr>
                </thead>
                <tbody>
                    <!-- 动态生成预览行 -->
                </tbody>
            </table>
            
            <div class="action-buttons">
                <button id="importBtn">导入银行流水</button>
            </div>
        </div>
    </div>

    <script>
        // 系统字段选项
        const systemFields = [
            { value: '', label: '不映射' },
            { value: 'tradingTime', label: '交易时间' },
            { value: 'tradingAmount', label: '交易金额' },
            { value: 'incomeAmount', label: '收入金额(情况一)' },
            { value: 'expenseAmount', label: '支出金额(情况一)' },
            { value: 'typeIdentifier', label: '类型标识(情况二/三)' },
            { value: 'balanceAmount', label: '账户余额' },
            { value: 'tradingType', label: '交易类型' },
            { value: 'status', label: '交易状态' },
            { value: 'channel', label: '支付渠道' },
            { value: 'bankName', label: '对方银行名称' },
            { value: 'bankNo', label: '对方银行卡号' },
            { value: 'counterparty', label: '交易对手方' },
            { value: 'payerBank', label: '付款人开户行名' },
            { value: 'payerAccount', label: '付款人账号' },
            { value: 'payerName', label: '付款人名称' },
            { value: 'payeeBank', label: '收款人开户行名' },
            { value: 'payeeAccount', label: '收款人账号' },
            { value: 'payeeName', label: '收款人名称' },
            { value: 'tradingDescription', label: '交易描述' },
            { value: 'remark', label: '备注' }
        ];

        // 字段类型选项
        const fieldTypes = [
            { value: 'normal', label: '普通字段' },
            { value: 'incomeAmount', label: '收入金额字段' },
            { value: 'expenseAmount', label: '支出金额字段' },
            { value: 'typeIdentifier', label: '类型标识字段' },
            { value: 'payerInfo', label: '付款人信息字段' },
            { value: 'payeeInfo', label: '收款人信息字段' }
        ];

        // 文件上传事件处理
        document.getElementById('bankFile').addEventListener('change', function(e) {
            const file = e.target.files[0];
            if (file) {
                // 显示上传文件信息
                document.getElementById('fileName').textContent = file.name;
                document.getElementById('uploadedFileInfo').style.display = 'block';
                
                // 这里应该调用后端API解析文件并返回列信息
                // 模拟数据（中国银行格式）
                const headerRowIndex = 8;
                const columns = [
                    { index: 0, name: '交易类型[ Transaction Type ]' },
                    { index: 1, name: '业务类型[ Business type ]' },
                    { index: 2, name: '付款人开户行号[ Account holding bank number of payer ]' },
                    { index: 3, name: '付款人开户行名[ Payer account bank ]' },
                    { index: 4, name: '付款人账号[ Debit Account No. ]' },
                    { index: 5, name: '付款人名称[ Payer\'s Name ]' },
                    { index: 6, name: '收款人开户行行号[ Account holding bank number of beneficiary ]' },
                    { index: 7, name: '收款人开户行名[ Beneficiary account bank ]' },
                    { index: 8, name: '收款人账号[ Payee\'s Account Number ]' },
                    { index: 9, name: '收款人名称[ Payee\'s Name ]' },
                    { index: 10, name: '交易日期[ Transaction Date ]' },
                    { index: 11, name: '交易时间[ Transaction time ]' },
                    { index: 12, name: '交易货币[ Trade Currency ]' },
                    { index: 13, name: '交易金额[ Trade Amount ]' },
                    { index: 14, name: '交易后余额[ After-transaction balance ]' },
                    { index: 15, name: '起息日期[ Value Date ]' },
                    { index: 16, name: '汇率[ Exchange rate ]' },
                    { index: 17, name: '交易流水号[ Transaction reference number ]' },
                    { index: 18, name: '客户申请号[ Online Banking Transaction Ref.(Bank Ref.) ]' },
                    { index: 19, name: '客户业务编号[ Customer Transaction Ref.(Customer Ref.) ]' },
                    { index: 20, name: '凭证类型[ Voucher type ]' },
                    { index: 21, name: '凭证号码[ Voucher number ]' },
                    { index: 22, name: '记录标识号[ Record ID ]' },
                    { index: 23, name: '摘要[ Reference ]' },
                    { index: 24, name: '用途[ Purpose ]' },
                    { index: 25, name: '交易附言[ Remark ]' },
                    { index: 26, name: '备注[ Remarks ]' },
                    { index: 27, name: '预留项1[ Reserve1 ]' },
                    { index: 28, name: '预留项2[ Reserve2 ]' },
                    { index: 29, name: '预留项3[ Reserve3 ]' },
                    { index: 30, name: '名义付款人开户行行号[ Opening bank number of nominal payer ]' },
                    { index: 31, name: '名义付款人开户行名[ Opening bank name of nominal payer ]' },
                    { index: 32, name: '名义付款人账号[ Payment A/C No. ]' },
                    { index: 33, name: '名义付款人名称[ Name of nominal payer ]' },
                    { index: 34, name: '名义收款人开户行号[ Opening bank number of nominal payee ]' },
                    { index: 35, name: '名义收款人开户行名[ Opening bank name of nominal payee ]' },
                    { index: 36, name: '名义收款人账号[ Account number of nominal payee ]' },
                    { index: 37, name: '名义收款人名称[ Name of nominal payee ]' }
                ];
                
                // 显示表头行索引信息
                document.getElementById('headerRowIndex').textContent = headerRowIndex;
                document.getElementById('headerRowIndexInfo').style.display = 'block';
                
                renderMappingTable(columns);
            }
        });

        // 渲染字段映射表格
        function renderMappingTable(columns) {
            const tbody = document.querySelector('#mappingTable tbody');
            tbody.innerHTML = '';
            
            columns.forEach(column => {
                const row = document.createElement('tr');
                
                // 文件列索引
                const indexCell = document.createElement('td');
                indexCell.textContent = column.index;
                row.appendChild(indexCell);
                
                // 文件列名称
                const nameCell = document.createElement('td');
                nameCell.textContent = column.name;
                row.appendChild(nameCell);
                
                // 映射到系统字段
                const mappingCell = document.createElement('td');
                const select = document.createElement('select');
                select.dataset.columnIndex = column.index;
                select.dataset.columnName = column.name;
                select.className = 'system-field-select';
                
                // 添加系统字段选项
                systemFields.forEach(field => {
                    const option = document.createElement('option');
                    option.value = field.value;
                    option.textContent = field.label;
                    select.appendChild(option);
                });
                
                mappingCell.appendChild(select);
                row.appendChild(mappingCell);
                
                // 字段类型
                const typeCell = document.createElement('td');
                const typeSelect = document.createElement('select');
                typeSelect.dataset.columnIndex = column.index;
                typeSelect.className = 'field-type-select';
                
                // 添加字段类型选项
                fieldTypes.forEach(type => {
                    const option = document.createElement('option');
                    option.value = type.value;
                    option.textContent = type.label;
                    typeSelect.appendChild(option);
                });
                
                typeCell.appendChild(typeSelect);
                row.appendChild(typeCell);
                
                tbody.appendChild(row);
            });
        }

        // 保存配置表单提交
        document.getElementById('configForm').addEventListener('submit', function(e) {
            e.preventDefault();
            
            const configData = {
                configName: document.getElementById('configName').value,
                bankType: document.getElementById('bankType').value,
                fileType: document.getElementById('fileType').value,
                remark: document.getElementById('remark').value
            };
            
            // 这里应该调用后端API保存配置
            console.log('保存配置:', configData);
            alert('配置保存成功！');
        });

        // 保存字段映射
        document.getElementById('saveMappingBtn').addEventListener('click', function() {
            const mappings = [];
            const systemSelects = document.querySelectorAll('#mappingTable .system-field-select');
            const typeSelects = document.querySelectorAll('#mappingTable .field-type-select');
            
            systemSelects.forEach((select, index) => {
                if (select.value) {
                    const typeSelect = typeSelects[index];
                    const mapping = {
                        columnIndex: parseInt(select.dataset.columnIndex),
                        columnName: select.dataset.columnName,
                        systemFieldName: select.value,
                        systemFieldDisplayName: systemFields.find(f => f.value === select.value)?.label || select.value,
                        fieldType: typeSelect.value || 'normal'
                    };
                    mappings.push(mapping);
                }
            });
            
            // 构造包含headerRowIndex的完整配置
            const headerRowIndex = parseInt(document.getElementById('headerRowIndex').textContent) || 0;
            const fullConfig = {
                headerRowIndex: headerRowIndex,
                mappings: mappings
            };
            
            // 这里应该调用后端API保存字段映射配置
            console.log('字段映射配置:', fullConfig);
            alert('字段映射保存成功！');
        });

        // 导入银行流水
        document.getElementById('importBtn').addEventListener('click', function() {
            const fileInput = document.getElementById('bankFile');
            const file = fileInput.files[0];
            
            if (!file) {
                alert('请先选择银行流水文件！');
                return;
            }
            
            // 这里应该调用后端API导入银行流水
            console.log('导入银行流水文件:', file.name);
            alert('银行流水导入成功！');
        });
    </script>
</body>
</html>